package com.easy.service.impl;

import com.easy.zhu.LogMethod;
import com.easy.bean.UserBean;
import com.easy.mapper.UserBeanDao;
import com.easy.service.userLoginService;
import com.easy.util.PasswordUtil;
import com.easy.util.SYS;
import com.easy.util.userTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;

@Service
public class userLoginServiceImpl implements userLoginService {
    @Autowired
    UserBeanDao userBeanDao;
    @Autowired
    private HandlerMappingIntrospector mvcHandlerMappingIntrospector;

    @Override
    @LogMethod(serviceName = "用户登录处理")
    public UserBean logininfo(String token) {
        //token中存的的id
        Object id= userTokenUtil.getOption(token,SYS.TOKEN_USER_ID);
        System.out.println(token);
        System.out.println(id);
        if(id==null){
            throw new RuntimeException("登陆异常");
        }
        UserBean userBean=userBeanDao.one(Integer.parseInt(id.toString()));
        return userBean;
    }

    @Override
    @LogMethod(serviceName = "用户登录处理")
    public String login(UserBean userBean) {
        //登录逻辑
        //1通过用户民查询数据库
        UserBean result=userBeanDao.getById(userBean.getUser_id());
        if(result==null){
            throw new RuntimeException("登陆异常：");
        }
        //2验证密码
        String password=result.getUser_code();
        String encryptionPassword= result.getUser_code();
        if(!PasswordUtil.checkPassword(userBean.getUser_code(),encryptionPassword)){
            throw new RuntimeException("登陆异常：密码错误");
        }
        //3返回结果token
        String token= userTokenUtil.getToken(result.getId(),"user");
        return token;


    }

    public static void main(String[] args) {
        String token = userTokenUtil.getToken("1","admin");
        System.out.println(token);
        System.out.println(userTokenUtil.verify(token));
        System.out.println(userTokenUtil.getOption(token, SYS.TOKEN_USER_ID));
    }
}































